//
//  SegmentPageView.m
//  Towerxi3
//
//  Created by MAC on 2021/5/10.
//

#import "SegmentPageView.h"

@implementation SegmentPageView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureView];
    }
    return self;
}

- (TXScrollView *)topScrollView {
    if (!_topScrollView) {
        _topScrollView = [[TXScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.topHeight)];
        _topScrollView.backgroundColor = UIColor.clearColor;
        _topScrollView.showsHorizontalScrollIndicator = NO;
        _topScrollView.showsVerticalScrollIndicator = NO;
        _topScrollView.scrollsToTop = NO;
        _topScrollView.delegate = self;
    }
    return _topScrollView;
}

- (TXScrollView *)bottomScrollView {
    if (!_bottomScrollView) {
        _bottomScrollView = [[TXScrollView alloc]initWithFrame:CGRectMake(0, self.topScrollView.bottom, self.width, self.height - self.topHeight)];
        _bottomScrollView.backgroundColor = UIColor.clearColor;
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        _bottomScrollView.showsVerticalScrollIndicator = NO;
        _bottomScrollView.scrollsToTop = NO;
        _bottomScrollView.pagingEnabled = YES;
        _bottomScrollView.delegate = self;
        _bottomScrollView.bounces = NO;
    }
    return _bottomScrollView;
}

- (NSMutableArray *)viewList {
    if (!_viewList) {
        _viewList = [NSMutableArray new];
    }
    return _viewList;
}

- (void)configureView {
    self.radius = 10.f;
    self.spacing = 0.f;
    self.topRadius = 0.f;
    self.topHeight = 40.f;
    self.equalWidth = NO;
    self.normalTextFont = FONT_SIZE(14);
    self.selectTextFont = FONT_BOLD_SIZE(16);
    self.normalTextColor = UIColorFromHex(@"707070");
    self.selectTextColor = UIColorFromHex(@"2C2C2C");
    self.normalBackColor = UIColorFromHex(@"FFFFFF");
    self.selectBackColor = UIColorFromHex(@"FFFFFF");

    [self addSubview:self.topScrollView];
    [self addSubview:self.bottomScrollView];
}

- (void)setNormalTextFont:(UIFont *)normalTextFont {
    _normalTextFont = normalTextFont;
}

- (void)setSelectTextFont:(UIFont *)selectTextFont {
    _selectTextFont = selectTextFont;
}

- (void)setNormalTextColor:(UIColor *)normalTextColor {
    _normalTextColor = normalTextColor;
}

- (void)setSelectTextColor:(UIColor *)selectTextColor {
    _selectTextColor = selectTextColor;
}

- (void)setNormalImages:(NSArray *)normalImages {
    _normalImages = normalImages;
}

- (void)setSelectImages:(NSArray *)selectImages {
    _selectImages = selectImages;
}

- (void)setNormalBackColor:(UIColor *)normalBackColor {
    _normalBackColor = normalBackColor;
}

- (void)setSelectBackColor:(UIColor *)selectBackColor {
    _selectBackColor = selectBackColor;
}

- (void)setTopRadius:(CGFloat)topRadius {
    _topRadius = topRadius;
}

- (void)setTopWidth:(CGFloat)topWidth {
    _topWidth = topWidth;
    self.topScrollView.frame = CGRectMake((self.width - topWidth)/2, 0, topWidth, self.topHeight);
}

- (void)setTopHeight:(CGFloat)topHeight {
    _topHeight = topHeight;
    self.topScrollView.frame = CGRectMake(0, 0, self.width, self.topHeight);
    self.bottomScrollView.frame = CGRectMake(0, self.topHeight, self.width, self.height - self.topHeight);
}

- (void)setTopBackColor:(UIColor *)topBackColor {
    _topBackColor = topBackColor;
}

- (void)setSpacing:(CGFloat)spacing {
    _spacing = spacing;
}

- (void)setRadius:(NSInteger)radius {
    _radius = radius;
}

- (void)setEqualWidth:(BOOL)equalWidth {
    _equalWidth = equalWidth;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
}

- (void)setTopAlignment:(SegmentTopAlignment)topAlignment {
    _topAlignment = topAlignment;
}

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers {
    _viewControllers = viewControllers;
    CGFloat width = 12;
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *viewController = viewControllers[i];
        viewController.view.frame = CGRectMake(self.width * i, 0, self.width, self.bottomScrollView.height);
        [self.bottomScrollView addSubview:viewController.view];

        UIFont *font = self.normalTextFont;
        UIColor *color = self.normalTextColor;
        UIColor *backColor = self.normalBackColor;
        UIImage *showImage = [UIImage imageNamed:self.normalImages.count?self.normalImages[i]:@""];
        if (i == self.selectIndex) {
            font = self.selectTextFont;
            color = self.selectTextColor;
            backColor = self.selectBackColor;
            showImage = [UIImage imageNamed:self.selectImages.count?self.selectImages[i]:@""];
        }
        
        /// 计算Title显示大小
        CGFloat titleWidth = [viewController.title textWidthWithFont:self.selectTextFont maxHeight:self.selectTextFont.pointSize];
        
        ViewsModel *model = [ViewsModel new];
        model.segmentType = self.segmentType;
        UIView *view = [UIView new];
        view.tag = i;
        view.backgroundColor = backColor;
        view.userInteractionEnabled = YES;
        [self.topScrollView addSubview:view];
        
        /// 文字
        if (self.segmentType == SegmentTitleType) {
            view.frame = CGRectMake(width, 0, titleWidth + 20.f, self.topHeight);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }
            UILabel *label = [UILabel new];
            label.font = font;
            label.textColor = color;
            label.text = viewController.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.frame = CGRectMake((view.width - titleWidth)/2, (view.height - self.selectTextFont.pointSize - 4)/2, titleWidth, self.selectTextFont.pointSize + 4);
            [view addSubview:label];
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(label.right, label.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.titleLabel = label;
            model.iconView = iconImage;
        }
        
        /// 带下划线的文字
        if (self.segmentType == SegmentBottomLineTitleType) {
            view.frame = CGRectMake(width, 0, titleWidth + 20.f, self.topHeight);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }

            UILabel *label = [UILabel new];
            label.font = font;
            label.textColor = color;
            label.text = viewController.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.frame = CGRectMake((view.width - titleWidth)/2, (view.height - self.selectTextFont.pointSize - 4)/2, titleWidth, self.selectTextFont.pointSize + 4);
            [view addSubview:label];
            
            UIView *line = [UIView new];
            line.hidden = YES;
            line.backgroundColor = UIColorFromHex(@"3080EE");
            line.frame = CGRectMake(label.left, label.bottom - 2, label.width, 1);
            [view addSubview:line];
            if (i == self.selectIndex) {
                line.hidden = NO;
            }
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(label.right, label.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.lineView = line;
            model.titleLabel = label;
            model.iconView = iconImage;
        }

        /// 带背景色文字
        if (self.segmentType == SegmentBackColorTitleType) {
            view.frame = CGRectMake(width, (self.topHeight - 30.f)/2, titleWidth + 40.f, 30.f);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }
            UILabel *label = [UILabel new];
            label.font = font;
            label.textColor = color;
            label.text = viewController.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.frame = CGRectMake((view.width - titleWidth)/2, 0, titleWidth, 30.f);
            [view addSubview:label];
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(label.right, label.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.titleLabel = label;
            model.iconView = iconImage;
            [view setCornerRadius:self.radius];
        }

        /// 图片
        if (self.segmentType == SegmentImageType) {
            /// 根据选中的图片计算图片显示大小
            UIImage *image = [UIImage imageNamed:self.selectImages[i]];
            view.frame = CGRectMake(width, 0, image.size.width + 20.f, self.topHeight);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }
            UIImageView *imageView = [UIImageView new];
            imageView.image = showImage;
            imageView.frame = CGRectMake((view.width - image.size.width)/2, (self.topHeight - image.size.height), image.size.width, image.size.height);
            [view addSubview:imageView];
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(imageView.right, imageView.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.imageView = imageView;
            model.iconView = iconImage;
        }

        /// 图片文字（图片居左）
        if (self.segmentType == SegmentLeftImageTitleType) {
            /// 根据选中的图片计算图片显示大小
            UIImage *image = [UIImage imageNamed:self.selectImages[i]];
            view.frame = CGRectMake(width, 0, image.size.width + titleWidth + 25.f, self.topHeight);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }
            UIImageView *imageView = [UIImageView new];
            imageView.image = showImage;
            imageView.frame = CGRectMake((view.width - image.size.width - titleWidth - 5)/2, (view.height - image.size.height)/2, image.size.width, image.size.height);
            [view addSubview:imageView];
            UILabel *label = [UILabel new];
            label.font = font;
            label.textColor = color;
            label.text = viewController.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.frame = CGRectMake(imageView.right + 5, (view.height - self.selectTextFont.pointSize - 4)/2, titleWidth, self.selectTextFont.pointSize + 4);
            [view addSubview:label];
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(imageView.right, imageView.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.titleLabel = label;
            model.imageView = imageView;
            model.iconView = iconImage;
        }

        /// 图片文字（图片居上）
        if (self.segmentType == SegmentTopImageTitleType) {
            /// 根据选中的图片计算图片显示大小
            UIImage *image = [UIImage imageNamed:self.selectImages[i]];
            view.frame = CGRectMake(width, 0, titleWidth + 20.f, self.topHeight);
            if (self.equalWidth) {
                CGFloat viewWidth = (self.topWidth - self.spacing * (viewControllers.count))/viewControllers.count;
                view.frame = CGRectMake(width, 0, viewWidth, self.topHeight);
            }
            UIImageView *imageView = [UIImageView new];
            imageView.image = showImage;
            imageView.frame = CGRectMake((view.width - image.size.width)/2, (view.height - image.size.height - self.selectTextFont.pointSize - 9)/2, image.size.width, image.size.height);
            [view addSubview:imageView];
            UILabel *label = [UILabel new];
            label.font = font;
            label.textColor = color;
            label.text = viewController.title;
            label.textAlignment = NSTextAlignmentCenter;
            label.frame = CGRectMake((view.width - titleWidth)/2, imageView.bottom + 5, titleWidth, self.selectTextFont.pointSize + 4);
            [view addSubview:label];
            
            UIImageView *iconImage = [UIImageView new];
            iconImage.frame = CGRectMake(imageView.right, imageView.top, 8, 8);
            iconImage.backgroundColor = UIColor.redColor;
            [iconImage setCornerRadius:4];
            [view addSubview:iconImage];
//            if (viewController.remindNum) {
//                iconImage.hidden = NO;
//            } else {
//                iconImage.hidden = YES;
//            }
            
            model.backView = view;
            model.titleLabel = label;
            model.imageView = imageView;
            model.iconView = iconImage;
        }
        
        width = width + view.width + self.spacing;
        [self.viewList addObject:model];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapAction:)];
        [view addGestureRecognizer:tap];
    }
    
    if (self.equalWidth) {
        self.topScrollView.backgroundColor = self.topBackColor;
        [self.topScrollView setCornerRadius:self.topRadius];
    } else {
        if (width < self.width) {
            /// 头部居左
            if (self.topAlignment == SegmentAlignmentLeft) {
                self.topScrollView.frame = CGRectMake(0, 0, width, self.topHeight);
            }
            
            /// 头部居中
            if (self.topAlignment == SegmentAlignmentCenter) {
                self.topScrollView.frame = CGRectMake((self.width - width)/2, 0, width, self.topHeight);
            }
            
            /// 头部居右
            if (self.topAlignment == SegmentAlignmentRight) {
                self.topScrollView.frame = CGRectMake(self.width - width, 0, width, self.topHeight);
            }
        }
    }
    
    self.topScrollView.contentSize = CGSizeMake(width + 12, self.topHeight);
    self.bottomScrollView.contentSize = CGSizeMake(self.width * self.viewControllers.count, self.height - self.topHeight);
    self.bottomScrollView.contentOffset = CGPointMake(self.selectIndex * self.width, 0);
}

- (void)viewTapAction:(UITapGestureRecognizer *)tap {
    [self selectIndex:tap.view.tag];
    [self.bottomScrollView setContentOffset:CGPointMake(tap.view.tag * SCWidth, 0)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bottomScrollView) {
        // 停止类型1、停止类型2
        BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (scrollToScrollStop) {
            [self scrollViewDidEndScroll:scrollView.contentOffset.x];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView == self.bottomScrollView) {
        if (!decelerate) {
           // 停止类型3
           BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
           if (dragToDragStop) {
               [self scrollViewDidEndScroll:scrollView.contentOffset.x];
           }
        }
    }
}

#pragma mark - scrollView 停止滚动监测
- (void)scrollViewDidEndScroll:(CGFloat)offsetx {
    /// 滚动了多少页
    int index = offsetx/SCWidth;
    
    [self selectIndex:index];
}

- (void)selectIndex:(NSInteger)index {
    ViewsModel *selectModel = self.viewList[index];
    
    /// 文字
    if (self.segmentType == SegmentTitleType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.titleLabel.font = self.selectTextFont;
                model.titleLabel.textColor = self.selectTextColor;
            } else {
                model.titleLabel.font = self.normalTextFont;
                model.titleLabel.textColor = self.normalTextColor;
            }
        }
    }
    
    /// 带下划线文字
    if (self.segmentType == SegmentBottomLineTitleType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.lineView.hidden = NO;
                model.titleLabel.font = self.selectTextFont;
                model.titleLabel.textColor = self.selectTextColor;
            } else {
                model.lineView.hidden = YES;
                model.titleLabel.font = self.normalTextFont;
                model.titleLabel.textColor = self.normalTextColor;
            }
        }
    }
    
    /// 带背景色文字
    if (self.segmentType == SegmentBackColorTitleType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.titleLabel.textColor = self.selectTextColor;
                model.backView.backgroundColor = self.selectBackColor;
            } else {
                model.titleLabel.textColor = self.normalTextColor;
                model.backView.backgroundColor = self.normalBackColor;
            }
        }
    }
    
    /// 图片
    if (self.segmentType == SegmentImageType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.imageView.image = [UIImage imageNamed:self.selectImages[index]];
            } else {
                model.imageView.image = [UIImage imageNamed:self.normalImages[model.backView.tag]];
            }
        }
    }
    
    /// 图片文字（图片居左）
    if (self.segmentType == SegmentLeftImageTitleType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.titleLabel.font = self.selectTextFont;
                model.titleLabel.textColor = self.selectTextColor;
                model.imageView.image = [UIImage imageNamed:self.selectImages[index]];
            } else {
                model.titleLabel.font = self.normalTextFont;
                model.titleLabel.textColor = self.normalTextColor;
                model.imageView.image = [UIImage imageNamed:self.normalImages[model.backView.tag]];
            }
        }
    }
    
    /// 图片文字（图片居上）
    if (self.segmentType == SegmentTopImageTitleType) {
        for (ViewsModel *model in self.viewList) {
            if (model.backView.tag == index) {
                model.titleLabel.font = self.selectTextFont;
                model.titleLabel.textColor = self.selectTextColor;
                model.imageView.image = [UIImage imageNamed:self.selectImages[index]];
            } else {
                model.titleLabel.font = self.normalTextFont;
                model.titleLabel.textColor = self.normalTextColor;
                model.imageView.image = [UIImage imageNamed:self.normalImages[model.backView.tag]];
            }
        }
    }
    
    if (self.topScrollView.contentSize.width > self.width) {
        if (selectModel.backView.x > self.width/2) {
            if (self.topScrollView.contentSize.width - (selectModel.backView.x + selectModel.backView.width/2) < self.width/2) {
                self.topScrollView.contentOffset = CGPointMake(self.topScrollView.contentSize.width - self.width, 0);
            } else {
                self.topScrollView.contentOffset = CGPointMake(selectModel.backView.x + selectModel.backView.width/2 - self.width/2, 0);
            }
        } else {
            self.topScrollView.contentOffset = CGPointMake(0, 0);
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(didSelectIndex:)]) {
        [self.delegate didSelectIndex:index];
    }
}

- (void)viewControllerRemind {
//    for (ViewsModel *model in self.viewList) {
//        UIViewController *viewController = self.viewControllers[model.backView.tag];
//        if (viewController.remindNum) {
//            model.iconView.hidden = NO;
//        } else {
//            model.iconView.hidden = YES;
//        }
//    }
}

@end

@implementation ViewsModel

@end
